package com.reesechou.controller.admin;

import com.reesechou.javabean.Link;
import com.reesechou.javabean.Tag;
import com.reesechou.service.CategoryService;
import com.reesechou.service.LinkService;
import com.reesechou.service.TagService;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by 小星打屁屁 on 2015/4/11.
 * contact me ==>243709758@qq.com
 */
@Controller
@RequestMapping("/jsp/admin/content")
public class LinkController {
   private static Logger logger =  Logger.getLogger(LinkController.class);

    private  static  String  LIST_PAGE = "link_list";
    private  static  String  ADD_PAGE = "link_add";

    @Autowired
    private LinkService linkService ;

    @RequestMapping("/link_list")
    public String getTagList(HttpServletRequest request ,HttpServletResponse response ,Model model){
        logger.info("跳转到标签分类的列表");
//        List<Category> list = categoryService.findAllCategory();
        /* 测试数据
        //TODO 删除 */
        Link link = new Link();
        link.setId(1);
        link.setTrash(false);
        link.setDescription("AAA");
        link.setName("ALINK");
        link.setSite("www.baidu.com");
        link.setStatus("1");
        Link link2 = new Link();
        link2.setId(2);
        link2.setTrash(false);
        link2.setDescription("BBB");
        link2.setName("BLINK");
        link2.setSite("www.sina.com");
        link2.setStatus("2");



        List<Link> list = new ArrayList<Link>();
        /* //TODO end */
        list.add(link);
        list.add(link2);
        model.addAttribute("links",list);
        return  LIST_PAGE ;
    }
    @RequestMapping(value = "/link_add",method = RequestMethod.GET)
    public String toTagAdd(HttpServletRequest request ,HttpServletResponse response ,Model model){
        logger.info("跳转到链接新增页面");
        //        List<Category> list = categoryService.findAllCategory();
        /* 测试数据
        //TODO 删除 */
        Link link = new Link();
        link.setId(1);
        link.setTrash(false);
        link.setDescription("CCC");
        link.setName("CLINK");
        link.setSite("www.baidu1.com");
        link.setStatus("1");
        Link link2 = new Link();
        link2.setId(2);
        link2.setTrash(false);
        link2.setDescription("DDD");
        link2.setName("DLINK");
        link2.setSite("www.sina1.com");
        link2.setStatus("2");

        List<Link> list = new ArrayList<Link>();
        /* //TODO end */
        list.add(link);
        list.add(link2);
        model.addAttribute("links",list);
        return ADD_PAGE ;
    }

    @RequestMapping(value = "/link_add",method = RequestMethod.POST)
    public String doTagAdd(HttpServletRequest request ,HttpServletResponse response ,Link link ,Model model){

        initLink(link);
        if(linkService.insert(link)){
            logger.info("添加链接成功");
        }else{
            logger.error("添加链接失败");
        }
        return "redirect:"+ADD_PAGE ;
    }

    public void initLink(Link link){


    }
}
